package net.shopxx.service.impl;

import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.shopxx.constant.AccountsConstant;
import net.shopxx.dao.ShuaDao;
import net.shopxx.dto.TbOrderDTO;
import net.shopxx.entity.Admin;
import net.shopxx.entity.Role;
import net.shopxx.entity.ShuaItem;
import net.shopxx.pager.AccountsPageable;
import net.shopxx.pager.Page;
import net.shopxx.service.AdminService;
import net.shopxx.service.ShuaService;

/**
 * Service - 账务处理服务
 * 
 * @author frankie
 * @version 3.0
 */
@Service("shuaServiceImpl")
public class ShuaServiceImpl extends BaseServiceImpl<ShuaItem, Long>implements ShuaService {
	
	@Resource(name = "shuaDaoImpl")
	private ShuaDao shuaDao;
	
	@Resource(name = "shuaDaoImpl")
	public void setBaseDao(ShuaDao shuaDao) {
		super.setBaseDao(shuaDao);
	}
	
	@Resource(name = "adminServiceImpl")
	private AdminService adminService;

	@Override
	@Transactional
	@CacheEvict(value = "shuaItem", allEntries = true)
	public void save(ShuaItem shuaItem) {
		if(StringUtils.isBlank(shuaItem.getOperator())){
			String operator = adminService.getCurrentUsername();
			shuaItem.setOperator(operator);
		}
		
		if(shuaItem.getStatus() == null){
			shuaItem.setStatus(AccountsConstant.STATUS_SHUADAN_INIT);
		}
		super.save(shuaItem);
	}

	@Override
	@Transactional
	@CacheEvict(value = "shuaItem", allEntries = true)
	public ShuaItem update(ShuaItem shuaItem) {
		return super.update(shuaItem);
	}

	@Override
	@Transactional
	@CacheEvict(value = "shuaItem", allEntries = true)
	public ShuaItem update(ShuaItem shuaItem, String... ignoreProperties) {
		return super.update(shuaItem, ignoreProperties);
	}

	@Override
	public Boolean judgeSave(ShuaItem shua) {
		
		Long exist = 0l;
		
		Calendar c = Calendar.getInstance();  
        c.add(Calendar.DATE, - 3);  
		
        exist = shuaDao.countRecentShuaItem(null, shua.getQq(), shua.getStoreId(), c.getTime(), null);
		if(exist > 0){
			return false;
		}
		c.add(Calendar.DATE, - 27);  
		exist = shuaDao.countRecentShuaItem(shua.getName(), null, shua.getStoreId(), c.getTime(), null);
		if(exist > 0){
			return false;
		}
		return true;
	}

	@Override
	@Transactional(readOnly = true)
    @Cacheable("shuaItem")
	public List<ShuaItem> getInitShuaOrder(TbOrderDTO order) {
		return shuaDao.getInitShuaOrder(order);
	}

	@Override
	public Boolean getEditPermission() {
		Admin admin = adminService.getCurrent();
		boolean editable = false;
		for(Role r : admin.getRoles()){
			if(r.getId() == AccountsConstant.SHUA_PERMISSION_EDIT){
				editable = true;
				break;
			}
		}
		return editable;
	}

	@Override
	public Page<ShuaItem> findStatsPage(AccountsPageable pageable) {
		return shuaDao.findStatsPage(pageable);
	}

	@Override
	public Float findStatsSum(AccountsPageable pageable) {
		return shuaDao.findStatsSum(pageable);
	}

	@Override
	public Long findStatsCount(AccountsPageable pageable) {
		return shuaDao.findStatsCount(pageable);
	}

	@Override
	public Float findCommissionSum(AccountsPageable pageable) {
		return shuaDao.findCommissionSum(pageable);
	}
}
